package cn.neusoft.interceptor;

import cn.neusoft.vo.ResultBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;

public class Loginterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("----------进入登录验证--------");
        //先获得这个session
        HttpSession session = request.getSession();
        Object user = session.getAttribute("adminUser");
        if (user == null) {
            //这里显示就没有登录成功
            //将错误写回去
            ObjectMapper objectMapper = new ObjectMapper();
            String  json = objectMapper.writeValueAsString(ResultBean.ERROR(5001,"未登录"));

            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(json);
            return false;
        }
        return true;
    }
}
